package com.bbx.taxi.client.DB.MyOrder;

import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 *我的订单locations表
 *
 */
public class MyOrderLocationsDB  implements DBcolumns{
	private DBHelper helper;
	public MyOrderLocationsDB(Context context) {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(context,myApplication.getUid());
	}
	/**
	 *  打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() { 
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 *  打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() { 
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}
	/**
	 * 根据order_id查询全部
	 */
	public Cursor onQuery(String order_id) {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_MYORDER_LOCATIONS, null,  MYORDER_LOCATIONS_ORDER_ID+"='"+order_id+"'", null, null,
				null, MYORDER_LOCATIONS_id + " ");
		return cursor;

	}
	/**
	 * 删除整个表
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_MYORDER_LOCATIONS, null, null);
		SQLdb.close();
	}

	/**
	 * 删除
	 */
	public void onDelete(String order_id) {
		SQLiteDatabase SQLdb=onOpenWrite();
		 int i =SQLdb.delete(TABLE_MYORDER_LOCATIONS, MYORDER_LOCATIONS_ORDER_ID + "='"+order_id+"'", null);        
		 SQLdb.close();
	}
	/**
	 *更新一条数据
	 */
	public void onUpdate(String id,String order_id,String start_address,String start_lng,String start_lat,String start_name,String start_phone
			,String end_address,String end_lng,String end_lat,String end_name,String end_phone,int volume,double weight,int count,int mutiple_oncar,int start_need_sms,int end_need_sms) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDER_LOCATIONS_ORDER_ID, order_id);
		cv.put(MYORDER_LOCATIONS_START_ADDRESS, start_address);
		cv.put(MYORDER_LOCATIONS_START_LNG, start_lng);
		cv.put(MYORDER_LOCATIONS_START_LAT, start_lat);
		cv.put(MYORDER_LOCATIONS_START_NAME, start_name);
		cv.put(MYORDER_LOCATIONS_START_PHONE, start_phone);
		cv.put(MYORDER_LOCATIONS_END_ADDRESS, end_address);
		cv.put(MYORDER_LOCATIONS_END_LNG, end_lng);
		cv.put(MYORDER_LOCATIONS_END_LAT, end_lat);
		cv.put(MYORDER_LOCATIONS_END_NAME, end_name);
		cv.put(MYORDER_LOCATIONS_END_PHONE, end_phone);
		cv.put(MYORDER_LOCATIONS_VOLUME, volume);
		cv.put(MYORDER_LOCATIONS_WEIGHT, weight);
		cv.put(MYORDER_LOCATIONS_COUNT, count);
		cv.put(MYORDER_MUTIPLE_ONCAR, mutiple_oncar);
		cv.put(MYORDER_START_NEED_SMS, start_need_sms);
		cv.put(MYORDER_END_NEED_SMS, end_need_sms);
		SQLdb.update(TABLE_MYORDER_LOCATIONS, cv, MYORDER_LOCATIONS_id + "=" + id, null);
		SQLdb.close();
	}
	/**
	 * 插入一条数据 
	 */
	public void onWrite(String order_id,String start_address,double start_lng,double start_lat,String start_name,String start_phone
			,String end_address,double end_lng,double end_lat,String end_name,String end_phone,int volume,double weight,int count,int mutiple_oncar) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(MYORDER_LOCATIONS_ORDER_ID, order_id);
		values.put(MYORDER_LOCATIONS_START_ADDRESS, start_address);
		values.put(MYORDER_LOCATIONS_START_LNG,String.valueOf(start_lng) );
		values.put(MYORDER_LOCATIONS_START_LAT, String.valueOf(start_lat));
		values.put(MYORDER_LOCATIONS_START_NAME, start_name);
		values.put(MYORDER_LOCATIONS_START_PHONE, start_phone);
		values.put(MYORDER_LOCATIONS_END_ADDRESS, end_address);
		values.put(MYORDER_LOCATIONS_END_LNG,String.valueOf(end_lng) );
		values.put(MYORDER_LOCATIONS_END_LAT, String.valueOf(end_lat));
		values.put(MYORDER_LOCATIONS_END_NAME, end_name);
		values.put(MYORDER_LOCATIONS_END_PHONE, end_phone);
		values.put(MYORDER_LOCATIONS_VOLUME, volume);
		values.put(MYORDER_LOCATIONS_WEIGHT, weight);
		values.put(MYORDER_LOCATIONS_COUNT, count);
		values.put(MYORDER_MUTIPLE_ONCAR, mutiple_oncar);
		SQLdb.insert(TABLE_MYORDER_LOCATIONS, MYORDER_LOCATIONS_id, values);
		SQLdb.close();
	}
	/**
	 * 关闭数据库
	 */
	public void onColse(){
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.close();
	}
}
